import gcRequest from '..'
import { ICreateProduct, productDetail, productsRes } from './types'

export const getProductsByCategoryId = (
  id: number,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<productsRes.RootObject>({
    url: `/product/category/${id}`,
    params: {
      offect,
      limit
    }
  })
}

export const getProducts = (offect: number, limit: number) => {
  return gcRequest.get<productsRes.RootObject>({
    url: '/product',
    params: {
      offect,
      limit
    }
  })
}

export const searchProduct = (
  word: string,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<productsRes.RootObject>({
    url: '/product/search',
    params: {
      word,
      offect,
      limit
    }
  })
}

export const getProduct = (id: number) => {
  return gcRequest.get<productDetail.RootObject>({
    url: `/product/${id}`
  })
}

export const createProduct = (data: ICreateProduct) => {
  return gcRequest.post<productDetail.RootObject>({
    url: '/product',
    data
  })
}

export const getProductsByUserId = (
  id: number,
  { offect, limit }: { offect: number; limit: number }
) => {
  return gcRequest.get<productsRes.RootObject>({
    url: `/product/user/${id}`,
    params: {
      offect,
      limit
    }
  })
}

export const deleteProductById = (id: number) => {
  return gcRequest.delete({
    url: `/product/${id}`
  })
}

export const updateProductById = (id: number, data: ICreateProduct) => {
  return gcRequest.patch<productDetail.RootObject>({
    url: `/product/${id}`,
    data
  })
}
